import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login/LoginIn.vue'
import Home from '../components/Home.vue'
const Welcome = () => import(/* webpackChunkName: "login_home_welome" */ '../components/welcome/Welcome')
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home', redirect: '/welcome', component: Home,
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: () => import('../components/users/Users.vue') },
        { path: '/users/detail/:id', component: () => import('../components/users/UserDetail.vue') },
        { path: '/content', component: () => import('../components/content/Content') },
        { path: '/consultation', component: () => import('../components/consultation/Consultation.vue') },
        { path: '/consultation/:id', component: () => import('../components/consultation/ConsultationForUser.vue') },
      ]
    },

  ]
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenstr = localStorage.getItem('token')
  if (!tokenstr) {
    Vue.prototype.$message.error('请先登录');
    return next('/login')
  }
  next()

})

export default router
